create proc [dbo].[sp_AB_AgingReport]
as

create table TempDueDate  (tempDueDate datetime)
insert into TempDueDate exec AB_GetDocDueDate

select
--------------------Company Details---------------------------------------
T1.CompnyName,T1.Phone1, T1.Fax, T1.FreeZoneNo, T1.TaxIdNum,
T1.CompnyAddr, T1.Block, T1.Street, T1.Country,T1.ZipCode,T1.LogoImage,
--------------------Outgoing Payment Details------------------------------ 
T0.DocEntry as DocNo, T0.CardCode, T0.CardName, T0.DocDate as PostingDate, T0.DocDueDate as DueDate,
isnull(T0.PrjCode,0)as ProjectCode,T0.OpenBal as BalanceDue
from OVPM T0 with(nolock)
---------------------------------------------------------------------------
join 
(
 select top(1) isnull(T0.PrintHeadr,T0.CompnyName) CompnyName,T0.Phone1, T0.Fax, T0.FreeZoneNo, T0.TaxIdNum,T1.ZipCode,
  T0.CompnyAddr,T1.Block,T1.Street,T3.LogoImage,
  T2.Name Country
 from OADM T0 with(nolock) 
 join ADM1 T1 with(nolock) on 1=1
 join OCRY T2 with(nolock) on T2.Code=T1.Country
 join OADP T3 with(nolock) on 1=1
) T1 on 1=1

where [docduedate] IN (SELECT * from  TempDueDate)
and OpenBal>0 and DocType='C'
order by T0.DocDueDate, T0.CardCode


drop table TempDueDate



